<createExampleNode>

[QUOTED FROM FILE types.115.xml]
Create a neural network where the root heapQueue is updated when a size-1 flo array (always at the same index) in any node is updated.
That is updated when a node "fires" and each of its childs flo are updated in the root heapQueue.
This should be done without defining "root". Use recursion.

net = a node containing all other nodes in this neural network. They are all in a heapQueue sorted by their value.
nodea = a neural node in net.
nodeb = a neural node in net. Also a child of nodea.
(net, nodea, and nodeb are 3 of the same type of node but with different            array sizes)

The purpose of this example neural network is to see how heapQueues can be used through deep recursion, not to create a useful neural network.

1 array in each of those nodes is a "virtual array" va thats parallel to the child node array ca. va is a view of the flo (in net's heapQueue) of each node in ca. Each node in ca is also in the heapQueue, but if it wasnt, the algorithm could be built to allow that.

Lots of types of virtual array could exist.
Example: a node array in the first node in this node's heapQueue
Example: a flo array in the first node in this node's heapQueue
Example: parent (in the current recursion) node's heapQueue
Example: Define recursion into child c by viewing all arrays in c as virtual arrays in this node. Any finite depth of recursion can be explicitly defined this way.
[END QUOTED FROM FILE types.115.xml]

<arraysOfExampleNode>
	int unique[1]
	int heapQueue[h]
	int heapQueueReverse[h]
	flo heapQueueValue[h]
	Object heapQueueNode[h]
	flo childWeight[c]
	Object child[c]
	Iter firstHeapFlo = flo from heapQueueValue, at first in heapQueue. //heapQueue Iters are always size 1.
	Iter firstHeapNode = Object from heapQueueNode, at first in heapQueue.
	Iter childWeightsOfFirstHeapNode = flo from childWeight of current Object in firstHeapNode.
	Iter childsOfFirstHeapNode = Object from child of current Object in firstHeapNode.
	Iter heapQueueNodeOfChildsOfFirstHeapNode = Object in heapQueueNode that equals current Object in childsOfFirstHeapNode. //TODO What if its not found? Should that be an error? Should it give index -1, index 0, null?
	Iter heapQueueValueOfChildsOfFirstHeapNode = flo in heapQueueValue at current index of heapQueueNodeOfChildsOfFirstHeapNode.
	Iter rootIterAndCodeOfExampleNode = ...defined below...
</arraysOfExampleNode>

<rootIterAndCodeOfExampleNode>
	//This will be done with Iter objects and other funcs. Dynamic-java-compile is not needed but can optimize it.
	if(firstHeapFlo > .5){ //The if is the first Iter.
		//The next statement "Weighted by..." is the second Iter and may be called by the first Iter.
		Weighted by childWeightsOfFirstHeapNode (or some statistical function of it),
			add total flo value 1 to the whole heapQueueValueOfChildsOfFirstHeapNode
			and subtract flo value 1 from firstHeapFlo
	}
</rootIterAndCodeOfExampleNode>

TODO This example only uses constant depth recursion but should also demonstrate variable depth recursion.
Do that after constant depth recursion is working.

Constant-depth recursion could be implemented as an int array the same size as the node, including the indexs containing Iters.
Each int in that array is a "current index".
Most Iters will change approximately 1/4 of those ints and ignore the others.
The ints can start at any valid value but will be more efficient if changed in the order defined by the running Iters because they will check if it changed to the expected index and only do the inefficient calculation if it was unexpected.
For example, a linear Iter used in a recursion may precompute the next 10 things, and use those 10 while the "current index" continues to increase by 1 each call of that linear Iter. If the "current index" changes to anything else, that precomputing is wasted, and if the Iter is efficient, it may choose to not precompute until the "current index" starts being more predictable.

Each Variable depth recursion needs a new int array the same size as the node. All nodes that can be reached from any 1 iteration are in the same network therefore are the same size Object array.

<arraysInConstantDepthRecursion>
	int unique[1] //every node needs 1 of these. This is not a node so maybe it should be removed.
	int currentIndex[n] //n means size of node including indexs of Iters
	int size[n] //Is this array needed?
	Object array[n] //All arrays in the node are copied here. Indexs of Iters can contain null or an array.
	//ERROR: Object array[n] does not contain the same type of thing, which is required for arraysInConstantDepthRecursion to be a node.
</arraysInConstantDepthRecursion>

Is arraysInConstantDepthRecursion fast enough to run mouse and speakers code with 20 size-1 flo arrays?
Nothing in arraysInConstantDepthRecursion would have to change for that, but how would the "mouse and speakers code" know that?
That is probably optimizable. This is a good design.

A nodeType is a node that describes the requirements of each instance of node's array sizes and array types and quantity of arrays.

Each nodeType has an array of Func.
Each Func is called with 3 parameters: int currentIndex[], int size[], Object array[].
Or should each Func be called with 1 parameter: an Object array containing those 3 arrays? This option makes code longer and slower.

Probably, each Func should be called with 3 parameters: int currentIndex[], int size[], Object array[].

Funcs that affect "virtual" things change the contents of int size[] and sometimes the contents of Object array[].

Iters are funcs that change the contents of int currentIndex[].

The replacement (in the previous design) for (or what uses) Flofuncs changes the contents of flo arrays in Object array[].
The most common types of mouse/speakers code will call Flofuncs on many size-1 flo arrays in Object array[],
and int currentIndex[] will contain all 0 and int size[] will contain all 1, but this is not required.
For example, some indexs could conditionally call more complex parts of the mouse/speakers code
every 64 times the mouse/speakers code runs.

All funcs should take 3 parameters (int currentIndex[], int size[], Object array[]).

To run a node, choose 1 of the funcs in its nodeType, create those 3 arrays based on array sizes in the node, and call the func on those 3 arrays.

//What should Func func[n] contain at indexs of arrays?
//Which Funcs define the type of an index or indexs?

//If func[3] takes 2 inputs, then funcInIndexArays[3] is a size-2 int array with 2 values between 0 and n-1.
//Are these index arrays too complex? They are only for information. The Func objects already know their indexs.
//Some indexs are input and output. Should that be a third category or way to describe it?
//Object funcInIndexArays[n]
//Object funcOutIndexArays[n]

<arraysInNodeType>
	int myInts[1] //unique int
	Object myTypes[1] //type of this typeNode
	Func func[n] //n is node size
</arraysInNodeType>

Should the Func.run function also take a Func array and run the child funcs from that instead of having them as childs directly?

REMOVE ALL NODETYPES FROM THIS DESIGN.
Should node[1] be a Func array instead of nodeType as Object array?
The current design is a nodeType contains a Func array and each node of that type contains that same nodeType.
Instead, the same Func array could be shared between all nodes of that type.
(or maybe some nodes could have their own Func array, but that would complicate things)

/** Should this class be immutable? */
public interface Func{

	/** parameters are the same as in arraysInConstantDepthRecursion (defined above) */
	public void run(int index[], int size[], Object array[]) throws Exception;

	//or should those parameters be combined into a Call object? Extra constructor call.
	//public void run(Call c);
	//or should those parameters be combined into an Object array? Extra casts and code to fill array.
	//public void run(Object callArrays[]);
	//or...
	//public void run(Object callArrays);

	/** Returns true if this Func has been optimized by compiling to Java bytecode, instead of interpreting as many Objects.
	The common ways to do that are (1) writing a Java class by hand or (2) Dynamic-compile with Javassist.
	TODO Should isCompiled be represented as being a subclass of some Java type, or maybe as some efficiency-info functions?
	*
	public boolean isCompiled();
	*/

	/** The run function takes arrays that are all the same size n.
	Returns an int array containing unique ints between 0 and n-1, which parts of the arrays to read and/or write.
	*
	public int[] getIndexs();
	*/

	/** Returns the nodeType that contains this Func or throws if it is not a member of a nodeType yet */
	public Object[] nodeType() throws Exception;

	public Func[] childs();

	/** Creates a new Func with specific child Funcs or throws if the childs
	are incompatible with this Func type or the quantity of childs is not allowed.
	newFunc(childs()) always creates a duplicate.
	*/
	public Func newFunc(Func childs[]) throws Exception;

}


public interface Func{

	public void run(int index[], int size[], Object aray[], Func func[]) throws Exception;

	/** index in nodeType that contains this Func. A Func is in at most 1 nodeType. */
	public int funcIndex();

	/** empty string or an informal description of this Func. Example: "bayesian weights that sum to 1" */
	public String natLangDescription();

}

There should be a behavior difference between array size requirements Funcs and Iter Funcs.
"array size requirements Funcs" should modify int size[],
and "Iter Funcs" should only read int size[] and read/write int index[].

Each Func should be able to tell you if it is optimized to assume specific ints (or all those it uses?) in int index[] and/or int size[] have certain values. For example, a bayesian node Func may be hard-coded to always use size-2 for a certain array.

Should Funcs be defined as a short String of code?:
"^(i5 i3) ??but how does it input and output its own int to size[] or index[]??"

Should Funcs have multiple ways to call them?
Example: *(^(b c) c) can read 3 (or 2?) sizes and write 1 size,      // sizes *(^(b c) c) --> size (b^c)*c
or read 3 (or 2?) sizes and read 1 index and write 3 (or 2?) indexs, // index (b^c)*c --> indexs *(^(b c) c) //may be wrong
or read 3 (or 2?) sizes and read 3 (or 2?) indexs and write 1 index. // indexs *(^(b c) c) --> index (b^c)*c //may be wrong
???                                                                  // index (b^c)*c --> index b

Optimization becomes much harder if the Func.run function has a Func array parameter because the Func at that index may change.
The 3 parameters should be: int index[], int size[], Object aray[].


I want to simplify the design of this example network, and possibly of networks in general if its flexible enough.


Arays occupy the first node.size indexs of arraysInConstantDepthRecursion.
Arays are connected by trees of Funcs branching toward lower index, but those trees are never traversed downward. They are only traversed upward to verify array sizes are correct, which is useful for choosing which new Funcs to add to a nodeType.


==Operators to calculate aray size are:==

size* is multiply.

size^ is power.

size= is the same size.


==Virtual array operators are:==

linearSearchForNode(arrayA arrayB) uses current index x in arrayA to get Object at index x in arrayA, find that Object in arrayB (if it exists there) at index y, and set current index for arrayB to y. Finds y where arrayA[x]==arrayB[y]. Error if its not found in arrayB.

logSearchForNode(arrayA arrayB) does the same as linearSearch== except the arrays must be sorted the same way as Audivolv's main node array: sorted by the int node[0][0]. You should use linearSearchForNode instead of this if arrayB is not sorted.

arayInChild(childNodes findWhatArayInChild replaceMeWithArayFromChild) only works if childNodes is nodes the same type as this node. findWhatArayInChild is an array in this node and all its childs. Its probably different sizes in each. This function finds the findWhatArayInChild of the current child (at the current index for the childNodes array of this node) and replaces replaceMeWithArayFromChild with that findWhatArayInChild. This function also sets the current index and current size for replaceMeWithArayFromChild. The current size is set to the actual aray size. The current index is more complex if it is allowed to interact with other arrays or iterations, but that may not be necessary. Maybe it should start at -1.

viewAray(arayA arayB) replaces arayB and its curSize with arayA and its curSize. Does not copy the contents of arayA.


==Operators that read and write current indexs but only read current sizes:==

//Not all of these operators will be inluded in the design of Audivolv.

index=(copyIndexFrom copyIndexTo)

remove^(bPowerC c setIndexOfBHere) //I dont like that this has 2 inputs and 1 output. It should have 1 input. Either way, the input(s) have to be certain types of operator.

remove^*(bPowerCThenMultC setIndexOfBHere) reads current sizes of b and c, reads current index of bPowerCThenMultC, and sets current index of setIndexOfBHere. This is useful in bayesian nodes.

factor*(bMultC b c) reads current size of c, reads current index of bMultC, and sets current indexs of b (to bMultC.index/c.size) and c (to bMultC.index%c.size). This is similar to the opposite of size* which calculates array sizes, but this calculates indexs.

factor^(bPowerCThenMultC bPowerC c setIndexOfBHere)
	//Same as factor*(bMultC b c) then remove^(bPowerC c setIndexOfBHere)

index*(b c replaceMeWithBMultC)


==Other types of operators==

pointer(intArrayA arrayB) copies an int value from intArrayA and overwrites the current index (not the value) of arrayB. This is used with a heapQueue. This func only works if intArrayA is an int array and contains ints from 0 or more to arrayB.length-1 or less.



<question importance=high>
	How to know if 2 specific funcs are compatible and can be used on the same array?
	Arays are leafs, which branch size operators toward lower index and are leafs in funcs from above. In both trees, it touches only at 1 places, but that place can be an expression of many functions (like (a*b)^(c*3)).
	size* is compatible with index*.
</question>

<question importance=high>
	How to use iterating funcs with the arayInChild(childNodes findWhatArayInChild replaceMeWithArayFromChild) func?
	That func gets a different array from a child node each time, so the total size of the iterator func is not easy to calculate (and is more complex if nodes can change childs during iterating, which should probably be prevented).
	Maybe that func should not be allowed to be a child of any iterating func?
	In this example node I'm designing, that func would only be used after a size-1 iteration to find the first node in a heapQueue.
	That func should be allowed to be a child of iterating funcs if those funcs do not change the size of the array that changed (replaceMeWithArayFromChild). That is consistent with the design of the example node because that node gets the array then iterates linearly over it.
	In general, "virtual" arrays should remain fixed size while they are being iterated over.
</question>

Each func should know which Funcs are its childs and be immutable.
Each func should be able to create new Funcs the same type as itself but with any childs, or throw if the childs are incompatible with that func type.

<question>
	Should each index in arraysInConstantDepthRecursion be the root of exactly 1 tree of funcs, and allow multiple parents?
	Creating view of array as virtualArray would still be needed often but the same rules apply to the virtual things.
	Example: Combine the size* func and the factor* func.
	Each func would have at most 2 ways to be called: to set the currentSize of the root, and to set the currentIndexs of the childs and call their roots (in depth-first order).
</question>


TODO This example node should be a hopfield node: http://en.wikipedia.org/wiki/Hopfield_network



<araysOfExampleNode>
	int myInts[1] //size is defined as constant 1
	Func myFuncs[araysInConstantDepthRecursion.length] //size is defined as constant, number of Funcs in this Node.

	int hq[h]        //size is defined in some range. Index always stays at 0 (or 1?).
	int hqReverse[h] //size func: size=(hq hqReverse)
	flo hqFlo[h]   //size func: size=(hq hqFlo)
	Ob hqNode[h] //size func: size=(hq hqNode)

	Ob child[c]    //size is defined in some range
	flo childWeight[c] //size func: size=(childWeight child)
	////////ABOVE ARRAYS ARE REAL, BELOW ARE VIRTUAL////////

	//Get first hq node 1 time.
	Ob hqCurNode[] //func: pointer(hq hqCurNode)
	flo hqCurValue[]   //func: pointer(hq hqCurValue)

	//Get variable-size child array of that node. Iterate over each child.
	Ob childOfHqCurNode[]    //func: arayInChild(hqCurNode child childOfHqCurNode)
	flo childWeightOfHqCurNode[] //func: arayInChild(hqCurNode childWeight childWeightOfHqCurNode)

	//Find a child in hq. This is 1 thing to do in the existing iteration.
	Ob hqNodeForChildOfHqCurNode[] is virtual view of hqNode[]. //func: logSearchForNode(childOfHqCurNode hqNodeForChildOfHqCurNode) //TODO What if its not found? Should that be an error? Should it give index -1, index 0, null?
	flo hqFloForChildOfHqCurNode[] is virtual view of hqFlo[]. //func: index=(hqNodeForChildOfHqCurNode hqFloForChildOfHqCurNode)

	//TODO use bit array (maybe size 1) to store the boolean value of if hqCurValue is big enough to continue

	? rootIterAndCodeOfExampleNode = ...defined below...
</araysOfExampleNode>

<Func>
	sequence(
		viewAray(hqNode hqNodeForChildOfHqCurNode) //hqNode goes in multiple places so we can have 2 curIndex for it.
		viewAray(hqFlo hqFloForChildOfHqCurNode) //hqFlo goes in multiple places so we can have 2 curIndex for it.
		viewAray(hqNode hqCurNode)
		viewAray(hqFlo hqCurValue)
		pointer(hq hqCurNode) //set curIndex of hqCurNode to the highest in the hq
		pointer(hq hqCurValue) //set curIndex of hqCurNode to the highest in the hq
		arayInChild(hqCurNode child childOfHqCurNode)
		arayInChild(hqCurNode childWeight childWeightOfHqCurNode)
		flo-=(hqCurValue ???totalAmountChange???)
		hqFloChanged(???hq??? ???hqReverse??? hqCurValue) //should this be merged with flo-=(...) above?
		recurse(
			childOfHqCurNode
			sequence(
				logSearchForNode(childOfHqCurNode hqNodeForChildOfHqCurNode)
				index=(hqNodeForChildOfHqCurNode hqFloForChildOfHqCurNode)
				flo+=(hqFloForChildOfHqCurNode ???totalAmountChangeDividedByChildQuantity???)
				hqFloChanged(???hq??? ???hqReverse??? hqFloForChildOfHqCurNode) //should this be merged with flo+=(...) above?
			)
		)
	)
</Func>

<rootIterAndCodeOfExampleNode>
	//This will be done with Iter objects and other funcs. Dynamic-java-compile is not needed but can optimize it.
	if(firstHeapFlo > .5){ //The if is the first Iter.
		//The next statement "Weighted by..." is the second Iter and may be called by the first Iter.
		Weighted by childWeightOfHqCurNode (or some statistical function of it),
			add total flo value 1 to all flos in hqFloForChildOfHqCurNode that are iterated over (which are only those the child node has child nodes for) and subtract flo value 1 from hqCurNode.
	}
</rootIterAndCodeOfExampleNode>

<question importance=high>
	Should each Func in a node be callable with 3 smaller arraysInConstantDepthRecursion arrays
	that only include the indexs that the tree of Funcs uses?
	It would need an extra array of immutable ints to specify which index in the node each index in arraysInConstantDepthRecursion points at. Those ints can only exist for arrays in the node. Some will have to be only for iterating.
	When a tree of Func is optimized as a Java class, its arraysInConstantDepthRecursion is smaller.
	The smallest it can get is number of arrays plus 1 (the root iterator).
	Different funcs can have different arraysInConstantDepthRecursion sizes, but all point into the
	same virtual space in the node. That size of a node includes only the real arrays in the node,
	and the virtual space includes the real arrays and as many higher indexs as needed.
</question>

public interface Func{

	public void run(int index[], int size[], Object aray[]) throws Exception;

	/** The run function has array parameters. This function tells which indexs can be modified in those arrays.
	Most functions use only a few of the indexs in those arrays
	because those arrays are usually bigger than the node the root Func is called on.
	The returned array never contains duplicate or negative ints.
	The returned array is not a backing-array. Changes to it have no effect on this Func.
	*/
	public int[] pointers();

	/** Returns a Func whose pointers() will return a copy of newPointers[].
	Throws if newPointers.length != pointers().length.
	f.changePointers(f.pointers()) returns a Func with the same behaviors as f.
	newPointers[] never contains duplicate or negative ints.
	newPointers[] is not a backing-array. Changes to it have no effect on the returned Func after its returned.
	If newPointers[] is the ints 0 to newPointers-1 (or should it be 2 to newPointers+1?), that is "normalized".
	*/
	public Func changePointers(int newPointers[]) throws Exception;

	/** empty string or an informal description of this Func. Example: "bayesian weights that sum to 1" *
	public String natLangDescription();
	*/

	/** All strings are UTF-32 encoded, 1 int per character. *
	public int[] natLang(int string[])
	*/

	//TODO Func should know its child Funcs

}


Mouse input and speakers output is done through a node. Add 4 size-1 flo arays to the node at low index,
but that input and output is not done by Funcs in a node because Funcs should be stateless.
It may is done externally. This allows mouse and speakers to be simulated or used with real hardware.



It is inefficient to use viewAray often, but probably it would have to be used often with this design.

Rewrite that specific Func in a syntax that does not duplicate array names just to loop over them.

Also, to test looping over a size bigger than any array, add an array called xxyyzz, size between 2 and 5, and define loop size as childOfHqCurNode.size*xxyyzz.size.

This should be a root Func with childs, not a sequence.

<Func>
	...TODO Rewrite that specific Func in a syntax that does not duplicate array names just to loop over them...
	sequence( //this is an important Func
		???The size of this sequence is 1. This is an array size 1 to specify that.???
		viewAray(hqNode hqNodeForChildOfHqCurNode) //hqNode goes in multiple places so we can have 2 curIndex for it.
		viewAray(hqFlo hqFloForChildOfHqCurNode) //hqFlo goes in multiple places so we can have 2 curIndex for it.
		viewAray(hqNode hqCurNode)
		viewAray(hqFlo hqCurValue)
		pointer(hq hqCurNode) //set curIndex of hqCurNode to the highest in the hq
		pointer(hq hqCurValue) //set curIndex of hqCurNode to the highest in the hq
		arayInChild(hqCurNode child childOfHqCurNode)
		arayInChild(hqCurNode childWeight childWeightOfHqCurNode)
		flo-=(hqCurValue ???totalAmountChange???)
		hqFloChanged(???hq??? ???hqReverse??? hqCurValue) //should this be merged with flo-=(...) above?
		size*(childOfHqCurNode xxyyzz theMultSize) //replaces theMultSize with a size childOfHqCurNode.size*xxyyzz.size
		loop( //Because childOfHqCurNode is recursively (using first index each time) size childOfHqCurNode, that is the loop size
			sequence( //this is an important Func
				logSearchForNode(childOfHqCurNode hqNodeForChildOfHqCurNode)
				index=(hqNodeForChildOfHqCurNode hqFloForChildOfHqCurNode)
				flo+=(hqFloForChildOfHqCurNode ???totalAmountChangeDividedByChildQuantity???)
				hqFloChanged(???hq??? ???hqReverse??? hqFloForChildOfHqCurNode) //should this be merged with flo+=(...) above?
			)
		)
	)
	...TODO Rewrite that specific Func in a syntax that does not duplicate array names just to loop over them...
</Func>


In the 3 arrays in "public void run(int index[], int size[], Object aray[]) throws Exception;",
for each index i in all 3 simultaneously,
should there be exactly 1 way to set index[i],
and exactly 1 way to set size[i] and/or aray[i],
and exactly 1 Func that iterates over the things at i (and at most 1 parent Func that calls it)?
That would make it harder to call Funcs in wrong orders that lose information created by a root Func call.


Should there be an extra array, in the Funcs and not added to those 3, that tells which index has the parent array,
and make one of the lower arrays be size 1 and contain the node being run?
viewAray(hqNode hqCurNode) means the parent is the current node (index 0) and the child is hqNode, and that creates hqCurNode.
arayInChild(hqCurNode child childOfHqCurNode) means the parent is in hqCurNode, the child is in the child array, and that creates childOfHqCurNode.


</createExampleNode>
